package Wuzi;

import javax.sound.sampled.*;
import java.io.IOException;
import java.io.InputStream;

public class MusicPlay {
    private static Clip bgm;//背景乐
    private static Clip hit;//音效
    private static AudioInputStream ais;
    MusicPlay()
    { }
    public static void play_Music(){
        try {
            bgm=AudioSystem.getClip();
            InputStream is= MusicPlay.class.getClassLoader().getResourceAsStream("background.wav");
             //getclassLoader得到当前类的加载器.getResourceAsStream加载资源，只能加载wav的音乐格式
            if (is != null) {
                ais=AudioSystem.getAudioInputStream(is);//获取输入流
            }
            bgm.open(ais);
        } catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
            e.printStackTrace();
        }
        bgm.start();//开始播放
       bgm.loop(Clip.LOOP_CONTINUOUSLY);//循环播放
    }
    public static void Stop_music()
    {
        if(ais!=null)
            bgm.close();
    }
    public static void play_Hit(){
        try {
            hit=AudioSystem.getClip();
            InputStream iss= MusicPlay.class.getClassLoader().getResourceAsStream("jump.wav");

            AudioInputStream ai = null;
            if (iss != null) {
                ai=AudioSystem.getAudioInputStream(iss);
            }
            hit.open(ai);
        } catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
            e.printStackTrace();
        }
        hit.start();
       // bgm.loop(Clip.LOOP_CONTINUOUSLY);
    }

}
